<?php
/**
 * 周工作计划及总结
 * @author Yangl
 **/
namespace Admin\Controller;
use Common\Controller\WahahabaseController;
class ExecutingWeeklyReportController extends WahahabaseController{
	protected $labourplan_model;
	public function _initialize() {
		parent::_initialize();
		$this->deal_model = D("Common/ExecutingWeeklyReport");
		$this->labourplan_model = D("Common/LabourPlan");
		
		$this->check = array('code', 'report_year', 'report_type');
	}	
	/**
     *  施工
     */
    public function indexPage($where){
        $eq = array(
            "eq" => "=",
            "neq" => "!=",
            "gt" => ">",
            "egt" => ">=",
            "lt" => "<",
            "elt" => "<=",
            "like" => "LIKE"
        ); 
        // 当前登录人
        $adminId = session('ADMIN_ID');
        $adminGroup = getUserInfo($adminId);
        //权限设置 20171228
        $condition = "(
            (d.is_publish = 0 AND (d.add_user = $adminId OR ((SELECT department_id FROM hzzy_users WHERE id=$adminId) = d.add_department) 
            AND ((SELECT role_id FROM hzzy_users WHERE id=$adminId) IN('1','2','3'))))
            
            OR
            
            (d.is_publish = 1 AND 
              (
                (SELECT IFNULL(department_role,0) as `level` FROM hzzy_users u1 LEFT JOIN hzzy_department d1 ON u1.department_id = d1.id WHERE u1.id=$adminId) 
                < 
                (SELECT IFNULL(department_role,0) as `level` FROM hzzy_users u2 LEFT JOIN hzzy_department d2 ON u2.department_id = d2.id WHERE u2.id=d.add_user)
              ) 
              AND '$adminGroup[department_role]' in ('1','2','3')
            	OR ((SELECT department_id FROM hzzy_users WHERE id=$adminId) = d.add_department)
            )
            OR
            ((SELECT role_id FROM hzzy_users WHERE id=$adminId) = 0 AND d.is_publish = 1)
        )";
        foreach($where as $key => $v){
            if(count($v) > 1){
                $d = $eq[$v[0]] == NULL ? '=' : $eq[$v[0]];
                if($d != 'LIKE'){
                    $condition .= " AND $key $d '".$v[1]."'";
                } else {
                    $condition .= " AND $key $d '%".$v[1]."%'";
                }
            } else {
                $condition .= " AND $key = '$v'";
            }
        }
        $count=$this->deal_model->alias("d")->where($condition)->count();
        $page = $this->page($count, 20);
        
        $results = $this->deal_model->alias("d")
            ->field('d.*,u.real_name,Com.department_name')
            ->join(array(
                ' LEFT JOIN ' . C('DB_PREFIX') . 'users u ON d.add_user = u.id',
                ' LEFT JOIN ' . C('DB_PREFIX') . 'department Com ON d.add_department = Com.id'
            ))
            ->where($condition)
            ->order("d.add_time DESC")
            ->limit($page->firstRow, $page->listRows)
            ->select();
		//exit($this->deal_model->getLastSql());	
        $this->assign("page", $page->show('Admin'));

        $this->assign("results",$results);

        $this->display();
    }
	public function sg(){
		$where = array("d.is_deleted"=>array('neq', 1));
		$where['report_type'] = 1;
		$code = I('request.code');
		if($code){
			$where['code'] = array('like',"%$code%");
		}
		$this->getDepartLevel();
		$this->indexPage($where);
	}
	public function sg_add(){
		//调用当前用户的部门名称
		$deptid = session('deptid'); //当前用户部门ID
		$depname = M('department')->where(array('id'=>$deptid,'is_deleted'=>0))->getField('department_name');
		$this->assign('depname', $depname);
		
		$first_day_of_week = date('Y-m-d',mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y')));
		$end_day_of_week = date('Y-m-d',mktime(0,0,0,date('m'),date('d')+(7-date('w')),date('Y')));
		$this->assign('first', $first_day_of_week);
		$this->assign('end', $end_day_of_week);
		
		$this->display();
	}
    public function sgadd_post(){
    	if(IS_POST){
			$this->dataCheck();
			$data = array_merge($_POST, $this->saveData());
			if ($this->deal_model->create()!==false) {
				$result = $this->deal_model->add($data);
				if ($result!==false) {
					// 投入信息
					$this->saveLabourPlan(2, $result);		
					$this->success("添加成功！", U("ExecutingWeeklyReport/sg"));
				} else {
					$this->error("添加失败！");
				}
			} else {
				$this->error($this->deal_model->getError());
			}
		}
	}
	public function sg_view(){
		$this->getDepartLevel();
		$this->getinfo();
	}
	public function sg_edit(){
		$this->getinfo();

	}
	public function sgedit_post(){
		if (IS_POST) {
			$this->dataCheck();
			$data = array_merge($_POST, $this->saveData(2));
			if ($this->deal_model->create()!==false) {
				$result=$this->deal_model->save($data);
				if ($result !== false) {
					// 删除投入
					$this->deleteLabourPlan(2, I("post.id"));
					// 投入信息
					$this->saveLabourPlan(2, I("post.id"));
					$this->success("保存成功！", U("ExecutingWeeklyReport/sg"));
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->deal_model->getError());
			}
		}
	}
	public function getinfo(){
		$id = I('get.id',0,'intval');

		$metting = $this->deal_model->where(array("id"=>$id))->find();
		$this->assign("result", $metting);

		$labels = $this->labourplan_model->where(array("table_id"=>$id, "belong_to"=>2, "plan_type" => 1))->select();
		$this->assign("labels1", $labels);
		
		$labels = $this->labourplan_model->where(array("table_id"=>$id, "belong_to"=>2, "plan_type" => 2))->select();
		$this->assign("labels2", $labels);
		
		$labels = $this->labourplan_model->where(array("table_id"=>$id, "belong_to"=>2, "plan_type" => 3))->select();
		$this->assign("labels3", $labels);

		$userinfo = getUserInfo($metting[add_user]);
        $this->assign('Complay',$userinfo['department_name']);

		$this->display();
	}
	
	/**
     *  代建
     */
	public function dj(){
		$where = array("d.is_deleted"=>array('neq', 1));
		$where['report_type'] = 2;
		$code = I('request.code');
		if($code){
			$where['code'] = array('like',"%$code%");
		}
		$this->getDepartLevel();
		$this->indexPage($where);
	}
	public function dj_add(){
		$first_day_of_week = date('Y-m-d',mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y')));
		$end_day_of_week = date('Y-m-d',mktime(0,0,0,date('m'),date('d')+(7-date('w')),date('Y')));
		$this->assign('first', $first_day_of_week);
		$this->assign('end', $end_day_of_week);
		
		$this->display();
	}
    public function djadd_post(){
		$this->addPost(U("ExecutingWeeklyReport/dj"));
	}
	public function dj_view(){
		$this->getDepartLevel();
		$this->editdisplay();
	}
	public function dj_edit(){
		$this->editdisplay();
	}
	public function djedit_post(){
		$this->editPost(U("ExecutingWeeklyReport/dj"));
	}


	public function getinfos(){
		$id = I('get.id',0,'intval');

		$metting = $this->deal_model->where(array("id"=>$id))->find();
		$this->assign("metting", $metting);
		
		$results = $this->att_model->where(array("table_id"=>$id, "belong_to"=>2))->select();
		$this->assign("results", $results);
		
		$labels = $this->labourplan_model->where(array("table_id"=>$id, "belong_to"=>1, "plan_type" => 1))->select();
		$this->assign("labels1", $labels);
		
		$labels = $this->labourplan_model->where(array("table_id"=>$id, "belong_to"=>1, "plan_type" => 2))->select();
		$this->assign("labels2", $labels);
		
		$labels = $this->labourplan_model->where(array("table_id"=>$id, "belong_to"=>1, "plan_type" => 3))->select();
		$this->assign("labels3", $labels);

		$this->display();
	}




	#######################接口############################
	##施工周报接口
	public function getSgByAjax(){
	    // 当前登录人
        $adminId = session('ADMIN_ID');
        $adminGroup = getUserInfo($adminId);
        $condition = "(
            (d.is_publish = 0 AND (d.add_user = $adminId OR ((SELECT department_id FROM hzzy_users WHERE id=$adminId) = d.add_department) 
            AND ((SELECT role_id FROM hzzy_users WHERE id=$adminId) IN('1','2','3'))))
            
            OR
            
            (d.is_publish = 1 AND 
              (
                (SELECT IFNULL(department_role,0) as `level` FROM hzzy_users u1 LEFT JOIN hzzy_department d1 ON u1.department_id = d1.id WHERE u1.id=$adminId) 
                < 
                (SELECT IFNULL(department_role,0) as `level` FROM hzzy_users u2 LEFT JOIN hzzy_department d2 ON u2.department_id = d2.id WHERE u2.id=d.add_user)
              ) 
              AND '$adminGroup[department_role]' in ('1','2','3')
            OR ((SELECT department_id FROM hzzy_users WHERE id=$adminId) = d.add_department)
            )
        )";
		

        $where = $condition . "d.is_deleted<>1 and d.report_type=1";
        $info = $this->deal_model->alias("d")
            ->field('d.*,u.real_name,Com.department_name')
            ->join(array(
                ' LEFT JOIN ' . C('DB_PREFIX') . 'users u ON d.add_user = u.id',
                ' LEFT JOIN ' . C('DB_PREFIX') . 'department Com ON d.add_department = Com.id'
            ))
            ->where($condition)
            ->order("d.add_time DESC")
            ->limit(5)
            ->select();
			
/*
         //假设查看的文章-发送部门等级为5级, 当前用户部门等级3级 ， 5>3，所以能查看.  //等级1为最高级
        $where = "d.is_publish=1 and d.is_deleted=0 and d.report_type=1
                and (Com.department_role > '$adminGroup[department_role]' 
                    OR 
                    d.add_department='$adminGroup[department_id]' AND '$adminGroup[department_role]' IN ('1','2','3') )";

		$info = $this->deal_model->alias("d")
                ->field('d.*')
                ->join(array(
                    ' LEFT JOIN ' . C('DB_PREFIX') . 'users u ON d.add_user = u.id',
                    ' LEFT JOIN ' . C('DB_PREFIX') . 'department Com ON d.add_department = Com.id'
                ))
                ->where($where)
            	->order("d.add_time DESC")
				->limit(5)
				->select();
*/
		exit(json_encode(
			array(
				'rtnType' => 'T',
				'rtnData' => $info
			)
		));		
	}




	##代建周报接口
	public function getDjByAjax(){
		$info = $this->deal_model
				->where(array('is_publish' => 1, 'is_deleted' => 0,'report_type' => 2))
            	->order("add_time DESC")
				->limit(5)
				->select();
		exit(json_encode(
			array(
				'rtnType' => 'T',
				'rtnData' => $info
			)
		));		
	}
}



?>